package may.qf.util;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class PageHelper<T> {
    private Integer pageIndex;
    private Integer pageSize;
    private Integer totalCount;
    private Integer totalPage;
    private Integer startRow;
    private List<T> data;

    public PageHelper(Integer pageIndex, Integer pageSize, Integer totalCount) {
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        this.totalPage = this.totalCount % this.pageSize == 0 ? this.totalCount / this.pageSize : this.totalCount / this.pageSize + 1;
        if (this.pageIndex <= 0) {
            this.pageIndex = 1;
        }
        if (this.totalPage == 0) {
            this.totalPage = 1;
        }
        if (this.pageIndex > this.totalPage) {
            this.pageIndex = this.totalPage;
        }
        this.startRow = (this.pageIndex - 1) * this.pageSize;
    }
}
